/*
 * 
 *
 *
 */
package com.hboxs.asl.dao.impl;

import com.hboxs.asl.dao.MerchantDao;
import com.hboxs.asl.entity.Merchant;
import org.springframework.stereotype.Repository;

import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;

/**
 * Dao - 商家
 */
@Repository("merchantDaoImpl")
public class MerchantDaoImpl extends BaseDaoImpl<Merchant, Long> implements MerchantDao {

    @Override
    public Merchant findByUsername(String username) {
        if (username == null) {
            return null;
        }
        try {
            String jpql = "select merchants from Merchant merchants where lower(merchants.username) = lower(:username)";
            return entityManager.createQuery(jpql, Merchant.class).setFlushMode(FlushModeType.COMMIT).setParameter("username", username).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    @Override
    public Merchant findByCode(String code) {
        if (code == null) {
            return null;
        }
        try {
            String jpql = "select merchants from Merchant merchants where lower(merchants.code) = lower(:code)";
            return entityManager.createQuery(jpql, Merchant.class).setFlushMode(FlushModeType.COMMIT).setParameter("code", code).getSingleResult();
        } catch(NoResultException e) {
            return null;
        }
    }

}
